import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue';
import PersonalBlog from '../views/PersonalBlog.vue';
import LoginRegister from '../views/LoginRegister.vue';
import Header from "@/components/Header.vue";
import QuillTextWrite from "@/components/QuillTextWrite";
import QuillTextRead from "@/components/QuillTextRead";
import {getToken} from "@/api/token";
import { Message} from "element-ui";
import AuthorInfoPage from "@/views/AuthorInfoPage";
import SearchArticles from "@/views/SearchArticles";
import LkUITableDemo from "@/components/LkUITableDemo";

// 先保存原始的 push 方法
const originalPush = VueRouter.prototype.push;
// 重写 push 方法   防止重复push报错
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/index/home',
    // redirect: '/index',  // 测试环境
  },
  {
    path: '/index',
    name: 'Header',
    component: Header,
    children:[
        // 首页
      {
        path: '/index/home',
        name: 'home',
        component: Home
      },
        // 个人博客  自己的信息页面
      {
        path: '/index/personal-blog',
        name: 'personalBlog',
        component: PersonalBlog,
        children:[
          // 用户信息管理
          {
            path: '/index/personal-blog/userBasicInfo',
            name: 'userBasicInfo',
            component: () => import(/* webpackChunkName: "LSD.bighome" */ '../views/UserBasicInfo.vue'),
          },
          // 文章管理
          {
            path: '/index/personal-blog/articleManage',
            name: 'articleManage',
            component: () => import(/* webpackChunkName: "LSD.bighome" */ '../views/ArticleManage.vue'),
          },
          // 我的点赞
          {
            path: '/index/personal-blog/articleLike',
            name: 'articleLike',
            component: () => import(/* webpackChunkName: "LSD.bighome" */ '../views/ArticleLike.vue'),
          },
          // 我的收藏
          {
            path: '/index/personal-blog/articleStar',
            name: 'articleStar',
            component: () => import(/* webpackChunkName: "LSD.bighome" */ '../views/ArticleStar.vue'),
          },
          // 我的关注
          {
            path: '/index/personal-blog/articleFollowing',
            name: 'articleFollowing',
            component: () => import(/* webpackChunkName: "LSD.bighome" */ '../views/ArticleFollowing.vue'),
          },
          // 我的粉丝
          {
            path: '/index/personal-blog/articleFollower',
            name: 'articleFollower',
            component: () => import(/* webpackChunkName: "LSD.bighome" */ '../views/ArticleFollower.vue'),
          },
        ]
      },
        // 富文本编辑页面
      {
        path: '/index/edit-text',
        name: 'quillTextWrite',
        component: QuillTextWrite
      },
        // 富文本阅读页面
      {
        path: '/index/read-text',
        name: 'quillTextRead',
        component: QuillTextRead
      },
        // 作者信息页面  别人点进来的
      {
        path: '/index/author-info',
        name: 'authorInfo',
        component: AuthorInfoPage
      },
      {
        path: '/index/searchArticles',
        name: 'searchArticles',
        component: SearchArticles
      },
        // 测试页面
      {
        path: '/index/test',
        name: 'TEST',
        component: LkUITableDemo
      },
    ]
  },
  {
    path: '/login-register',
    name: 'loginRegister',
    component: LoginRegister
  },
]

const router = new VueRouter({
  mode: 'history',
  routes
})

/**
 * 路由跳转前的判断
 */
router.beforeEach((to, from, next) => {
  // 如果打开的不是login页面，都需要验证token
  if(to.name!=='loginRegister'){
    // 先检查token，除了login页面之外都需要判断token
    const token = getToken();
    // 判断token   token存在
    if(token!==null){
      next();
      // 使用 next({...to, replace: true }) 避免死循环和浏览器后退问题
    }
    // token不存在 跳转至登录页
    else { // token不存在
      Message.error("无效的会话，或者会话已过期，请重新登录。")
      next('/login-register')
    }
  }
  // 如果打开的是登陆页面   啥也不干 next即可
  else {
    next();
  }
});

export default router
